package leetcode;

/**
 * Created by Ghost on 2014/9/28 0028.
 */
public class Longest_Substring_Without_Repeating_Characters {
    public static int lengthOfLongestSubstring(String s) {
        if(s == null || s.length() == 0)
            return 0;
        int[] hash = new int[256];
        for(int i=0;i<256;i++)
            hash[i] = -1;
        String target = "";
        String tmpS = "";
        for(int i=0;i<s.length();i++){
            char tmp = s.charAt(i);
            if(hash[tmp] > -1){
                if(target.length()<tmpS.length()){
                    target = tmpS;
                }
                String tPre = tmpS.substring(0,tmpS.indexOf(tmp));
                for(int k=0;k<tPre.length();k++)
                    hash[tPre.charAt(k)] = -1;
                tmpS = tmpS.substring(tmpS.indexOf(tmp)+1)+tmp;
            }
            else{
                tmpS += tmp;
            }
            hash[tmp] = i;

        }
        return tmpS.length()>target.length()?tmpS.length():target.length();
    }
    public static void main(String[] args){
        System.out.println(lengthOfLongestSubstring("abcbc"));
    }
}
